package imse.team0.extapp.transportation.ws;

import imse.team0.transportation.ShipmentPriceReply;
import imse.team0.transportation.TransportationOrder;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/*CXF path creation -- see http://cxf.apache.org/docs/jax-rs.html#JAX-RS-HowRequestURIisMatched*/

/**
 * @author Eugen, 5/2/12 5:53 PM
 */
@Path("/")
//@Produces(MediaType.APPLICATION_XML+","+MediaType.APPLICATION_JSON)
//@Consumes(MediaType.APPLICATION_XML+","+MediaType.APPLICATION_JSON)
public class ShippingService {
    @POST
    @Path("computePrice")
    @Consumes(MediaType.APPLICATION_JSON)
    /*
    {"shipmentRequest": {
        "@id": "Package87",
        "@packageVolume": "5",
        "start-address": "2",
        "end-address": "4"
                        }
    }*/
    public ShipmentPriceReply getShipmentOrderPrice(TransportationOrder request) {
        System.out.println("Received shipment request:\n\tid="+request.shipmentRequestID+"\n\ts-addr="+request.startAddress+"\n\te-addr="+request.endAddress+"\n\tvolume="+request.loadVolume);
        String startAddress = request.startAddress;
        String endAddress = request.endAddress;
        double distance = getRouteDistance(startAddress, endAddress);
        double price = calculatePrice(request.loadVolume, distance);
        ShipmentPriceReply reply = new ShipmentPriceReply();
        reply.setPrice(price);
        reply.setShipmentRequestID(request.shipmentRequestID);
        return reply;
    }

    private double getRouteDistance(String start, String end) {
        return Math.abs(Double.parseDouble(start) - Double.parseDouble(end));
    }

    private double calculatePrice(double volume, double distance) {
        return costs(volume) * distance * 2.0;
    }

    private double costs(double volume) {
        return Math.sqrt(volume) + 10;
    }

    @PUT
    @Path("perform")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public String initiateShipment(TransportationOrder shipmentOrder) {
        System.out.println("Received shipment request:\n\tid="+shipmentOrder.shipmentRequestID+"\n\ts-addr="+shipmentOrder.startAddress+"\n\te-addr="+shipmentOrder.endAddress+"\n\tvolume="+shipmentOrder.loadVolume);
        String trackingNumber;
        // would do here some allocation of resource if where a real example
        trackingNumber = generateTrackingNumber();
        return trackingNumber;
    }

    private String generateTrackingNumber(){
        long time = System.currentTimeMillis();
        return time+"";
    }

    @POST
    @Path("/dummy")
    //@Consumes(MediaType.TEXT_PLAIN)
    //@Produces(MediaType.TEXT_PLAIN)
    public String testString(String string){
        //MultipleEndpointObserver observer = new MultipleEndpointObserver(new Bus());
        System.out.println("Received string=("+string+")");
        if (string == null){
            return "NULL BODY !!";
        }
        String body = string+": ZING !";
        return body;
    }
}
